<template>
  <div>
    <ych-modal
      :fullscreen="false"
      :title="title"
      :visible="visible"
      :destroyOnClose="true"
      :bodyStyle="bodyStyle"
      @ok="handleOk"
      @cancel="handleCancel"
      :cancelText="$t('m.common.button.close')">

      <a-tabs v-if="needUser && transitionTabs.length > 0" :tabPosition="tabPosition" size="small">
        <a-tabPane v-for="transition in transitionTabs" :key="transition.targetActivityId"
                   :tab="transition.targetActivityName" :forceRender="true">
          <bpm-select-user-data :transition-id="transition.id" :target-activity-id="transition.targetActivityId"
                                :task-id="taskId" :operate-type="transition.operateType"
                                :auto-select-user="transition.autoSelectUser"
                                :target-activity-name="transition.targetActivityName"
                                @changeSelectUser="changeSelectUser"
                                :height="height"
                                @changeDueDate="changeDueDate" :getActorsUrl="getActorsUrl"></bpm-select-user-data>
        </a-tabPane>
      </a-tabs>

      <bpm-select-user-data v-if="needUser && transitionTabs_hide.length > 0" v-show="false"
                            v-for="transition in transitionTabs_hide" :key="transition.targetActivityId"
                            :transition-id="transition.id" :target-activity-id="transition.targetActivityId"
                            :task-id="taskId" :operate-type="transition.operateType"
                            :auto-select-user="transition.autoSelectUser"
                            :target-activity-name="transition.targetActivityName"
                            @changeSelectUser="changeSelectUser" @changeDueDate="changeDueDate"
                            :getActorsUrl="getActorsUrl"></bpm-select-user-data>

      <bpm-select-user-data v-show="!hideUserDialog" :auto-select-user="autoSelectUser"
                            v-if="needUser && transitionTabs.length == 0 && transitionTabs_hide.length == 0"
                            :transition-id="transitionId"
                            :target-activity-id="targetActivityId" :task-id="taskId" :operate-type="operateType"
                            :target-activity-name="nextActivityName"
                            @changeSelectUser="changeSelectUser" @changeDueDate="changeDueDate"
                            :height="height"
                            :getActorsUrl="getActorsUrl"></bpm-select-user-data>

      <bpm-message-drawer v-if="needMessage" @setMessage="setMessage"
                          :message="returnData.message"></bpm-message-drawer>
      <a-textarea v-if="needMessage" v-model="returnData.message" :placeholder="$t('m.bpm.design.idea_required_placeholder')" :rows="4"></a-textarea>
    </ych-modal>

    <div v-if="hideUserDialog && !visible">
      <bpm-select-user-data v-if="needUser && transitionTabs_hide.length > 0" v-show="false"
                            v-for="transition in transitionTabs_hide" :key="transition.targetActivityId"
                            :transition-id="transition.id" :target-activity-id="transition.targetActivityId"
                            :task-id="taskId" :operate-type="transition.operateType"
                            :auto-select-user="transition.autoSelectUser"
                            :target-activity-name="transition.targetActivityName"
                            @changeSelectUser="changeSelectUser" @changeDueDate="changeDueDate"
                            :getActorsUrl="getActorsUrl"></bpm-select-user-data>

      <bpm-select-user-data v-show="!hideUserDialog" :auto-select-user="autoSelectUser"
                            v-if="needUser && transitionTabs.length == 0 && transitionTabs_hide.length == 0"
                            :transition-id="transitionId"
                            :target-activity-id="targetActivityId" :task-id="taskId" :operate-type="operateType"
                            :target-activity-name="nextActivityName"
                            @changeSelectUser="changeSelectUser" @changeDueDate="changeDueDate"
                            :getActorsUrl="getActorsUrl"></bpm-select-user-data>
    </div>
  </div>
</template>

<script>

  import BpmSelectUserData from './BpmSelectUserData'
  import BpmMessageDrawer from './BpmMessageDrawer'

  export default {
    name: 'BpmSelectUserDialog',
    components: {
      BpmSelectUserData,
      BpmMessageDrawer
    },
    props: {
      getActorsUrl: {
        type: String,
        default: null
      },
    },
    data() {
      return {
        title: this.$t('m.bpm.other.process_dialog'),
        visible: false,
        tabPosition: 'left',
        needUser: false,
        needMessage: true,
        mustIdea: false,
        hideUserDialog: false,
        autoSelectUser: false,
        transitionTabs: [],
        transitionTabs_hide: [],
        transitionId: null,
        taskId: null,
        operateType: null,
        targetActivityId: null,
        validTransition: [],
        nextActivityName: null,
        returnData: {
          message: null,
          userList: [],
          userListMap: {},
          dueDateMap: {}
        },
        filterUserList: [],
        clientHeight: document.body.clientHeight,
        bodyStyle: {},
      }
    },
    computed: {
      height() {
        const result = this.clientHeight - (this.needMessage ? 585 : (585 - 135))
        return result > 200 ? result : 200;
      }
    },
    methods: {
      init(button, activity) {
        this.title = button.label
        if (activity) {
          this.title += "—" + activity.name;
        }
        this.needUser = false
        this.needMessage = true
        this.mustIdea = (button.attribute && button.mustIdea == '1')
        this.hideUserDialog = false
        this.autoSelectUser = false
        this.transitionTabs = []
        this.transitionTabs_hide = []
        this.transitionId = null
        this.taskId = button.attribute.taskId
        this.operateType = button.type
        this.targetActivityId = button.attribute.targetActivityId
        this.validTransition = []
        this.nextActivityName = null
        this.returnData = {
          message: null,
          userList: [],
          userListMap: {},
          dueDateMap: {}
        }
        this.returnData.message = button.attribute.defaultMessage || null
        this.filterUserList = []
      },
      open(button, activity) {
        this.init(button, activity)
        this.needUser = true
        this.visible = true
      },
      openNoMessage(button, activity) {
        this.init(button, activity)
        this.needUser = true
        this.needMessage = false
        this.visible = true
      },
      openNoUser(button, hideIdeaDialog, activity) {
        this.init(button, activity)
        this.needUser = false
        this.needMessage = !hideIdeaDialog;

        if (hideIdeaDialog) {
          const that = this
          this.$confirm({
            title: this.$t('m.common.title.operate'),
            content: this.$t('m.bpm.verify.que-ren-ban-li-ma'),
            onOk: function () {
              that.handleOk()
            },
            onCancel: function () {
              that.init(button, activity)
            }
          })
        } else {
          this.visible = true
        }
      },
      openForSubmit(button, transition, hideIdeaDialog, activity) {
        this.init(button, activity)
        const that = this
        this.needMessage = !hideIdeaDialog;
        this.nextActivityName = transition.targetActivityName
        this.transitionId = transition.id
        this.targetActivityId = transition.targetActivityId
        this.returnData.userListMap [transition.id] = []
        this.returnData.dueDateMap[transition.id] = []
        if (transition.targetActivityType.indexOf('Gateway') != -1) {
          this.nextActivityName = null
        }
        if (transition.needSelectUser) {
          this.needUser = true
          this.autoSelectUser = !!(transition.autoSelectUser)
          this.hideUserDialog = !!(transition.hideUserDialog)
          if (transition.mustSelectUser) {
            this.validTransition.push(transition.id)
          }
        } else if (transition.nextTransitionList.length > 0) {
          transition.nextTransitionList.forEach((item) => {
            that.returnData.userListMap [item.id] = []
            that.returnData.dueDateMap [item.id] = []
            if (item.needSelectUser) {
              if (item.hideUserDialog) {
                that.transitionTabs_hide.push(item)
              } else {
                that.transitionTabs.push(item)
              }
            }
            if (item.mustSelectUser) {
              that.validTransition.push(item.id)
            }
          })
          if (this.transitionTabs.length == 1 && this.transitionTabs_hide.length == 0 && this.transitionTabs[0].id != 'addPassRound') {
            this.transitionId = this.transitionTabs[0].id
            this.targetActivityId = this.transitionTabs[0].targetActivityId
            this.nextActivityName = this.transitionTabs[0].targetActivityName
            this.autoSelectUser = !!(this.transitionTabs[0].autoSelectUser)
            this.hideUserDialog = !!(this.transitionTabs[0].hideUserDialog)
            this.transitionTabs = []
            this.needUser = true
          } else if (this.transitionTabs.length > 0 || this.transitionTabs_hide.length > 0) {
            this.needUser = true
          }

          if (this.transitionTabs.length == 0 && this.transitionTabs_hide.length > 0) {
            this.hideUserDialog = true;
          }
        }
        if (this.nextActivityName) {
          this.title = this.title + this.$t('m.bpm.other.xia-yi-bu') + this.nextActivityName
        }

        if (!this.needMessage && this.hideUserDialog) {
          const that = this
          this.$confirm({
            title:this.$t('m.common.title.operate'),
            content: this.$t('m.bpm.verify.que-ren-ban-li-ma'),
            onOk: function () {
              that.handleOk()
            },
            onCancel: function () {
              that.init(button, activity)
            }
          })
        } else {
          this.visible = true
        }
      },
      changeSelectUser(selectedRowKeys, transitionId, filterUserSet) {
        if (transitionId) {
          this.returnData.userListMap[transitionId] = selectedRowKeys
        } else {
          this.returnData.userList = selectedRowKeys
          this.filterUserList = filterUserSet
        }
      },
      changeDueDate(dueDate, transitionId) {
        if (transitionId) {
          this.returnData.dueDateMap[transitionId] = dueDate
        }
      },
      setMessage(message) {
        this.returnData.message = message
      },
      handleOk() {
        if (this.needUser) {
          if (!this.transitionId) {
            if (this.returnData.userList.length == 0) {
              this.messageUtil.warning(this.$t('m.bpm.verify.qing-xuan-ren'))
              return
            }
            let selectedUsers = this.returnData.userList.filter(item => this.filterUserList.indexOf(item) == -1)
            if (selectedUsers.length == 0) {
              this.messageUtil.warning(this.$t('m.bpm.verify.user_select_error'))
              return
            }
            this.returnData.userList = selectedUsers

          } else {
            const that = this
            let valid_flg = true
            this.validTransition.forEach(function (item) {
              if (that.returnData.userListMap[item].length == 0) {
                valid_flg = false
              }
            })
            if (!valid_flg) {
              this.messageUtil.warning(this.$t('m.bpm.verify.qing-xuan-ren'))
              return
            }
          }
        }
        if (this.mustIdea && this.needMessage && !this.returnData.message) {
          this.messageUtil.warning(this.$t('m.bpm.verify.qing-tian-xie-yi-jian'))
          return
        }
        this.$emit('selectUserBack', this.returnData)
        this.handleCancel()
      },
      handleCancel() {
        this.visible = false
        //为了关闭候选人数据，再次打开时重新加载
        this.needUser = false
        this.hideUserDialog = false
      },
      handleResize() {
        this.clientHeight = document.body.clientHeight
      }
    },
    created() {
      //console.log("-------------------------", this.clientHeight)
      window.addEventListener('resize', this.handleResize)
    },
    beforeDestroy() {
      window.removeEventListener('resize', this.handleResize)
    }
  }
</script>
